local qunxian3__hanniba_yanqing = fk.CreateSkill {
  name = "qunxian3__hanniba_yanqing",
}

Fk:loadTranslationTable {
  ["qunxian3__hanniba_yanqing"] = "宴请",
  [":qunxian3__hanniba_yanqing"] = [[出牌阶段，若你拥有【食物】标记，则你选择所有存活角色，令其选择一种【食物】标记，然后根据标记令其获得特殊效果直至你的下个额定出牌阶段开始时：
<br/>友方食用（豆腐花）时，使用牌时可以弃置一种花色的所有手牌，然后从牌堆里获得一张指定装备牌，
<br/>敌方食用（豆腐花）时，每次使用牌时扣除五分之一（向下取整且至少为1）体力上限的体力，

<br/>友方食用（炸翅膀）时，下一个摸牌阶段结束时可以弃置至多四张手牌中的装备牌，本回合内视为获得弃置装备牌的技能，
<br/>敌方食用（炸翅膀）时，每使用一张牌便弃置一张牌，每次使用一张牌需要弃置两张牌才能生效，

<br/>友方食用（牛排）时，恢复体力上限一半(向下取整)的体力并增加三点体力上限，
<br/>敌方食用（牛排）时，回合开始时弃置一种类型的手牌并本回合内不可使用此类牌，

<br/>友方食用（煎蛋）时本回合获得一个随机技能，回合结束时失去以此法获得的技能，
<br/>敌方食用（煎蛋）失去一个技能并令本回合内使用牌只能指定为距离1的目标]],
  ["@[qunxian3__hanniba_yanqing]"] = "宴请",
  ['#qunxian3__hanniba_yanqing-choose'] = '宴请:请选择一种食物',
  ["#qunxian3__hanniba_yanqing-friend_doufuhua"] = "宴请：你可以弃置一种花色的所有手牌，然后从牌堆里获得一张指定装备牌",
  ["#qunxian3__hanniba_yanqing-enemy_zhachibang"] = "宴请：你需要弃置两张牌才能令此牌生效",
  ["#qunxian3__hanniba_yanqing-zhachibang_good"] = "宴请：你可以弃置至多四张装备牌，本回合内视为获得弃置装备牌的技能",
  ["@[:]qunxian3__hanniba_yanqing-zhachibang_good"] = "炸翅膀",
  ["#qunxian3__hanniba_yanqing-niupai_bad"] = "烹饪：选择一张手牌，弃置此类型所有手牌并本回合内不可使用此类牌"
}

qunxian3__hanniba_yanqing:addEffect("active", {
  anim_type = "support",
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:hasSkill(qunxian3__hanniba_yanqing.name) and #player:getTableMark("@[qunxian3__hanniba_pengren]") > 0
        and #table.filter(Fk:currentRoom().alive_players, function(p, index, array)
          return #p:getTableMark("@[qunxian3__hanniba_yanqing]") < 4
        end) > 0
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    local shiwu_mark = player:getTableMark("@[qunxian3__hanniba_pengren]")
    local yanqing_players = table.filter(room.alive_players, function(p, index, array)
      return #p:getTableMark("@[qunxian3__hanniba_yanqing]") < 4
    end)
    local choice_players = room:askToJointChoice(player, {
      players = yanqing_players,
      choices = shiwu_mark,
      skill_name = "qunxian3__hanniba_yanqing",
      prompt = "#qunxian3__hanniba_yanqing-choose"
    })
    for _player, choice in pairs(choice_players) do
      if choice == "牛排" and player:isFriend(_player) then
        room:recover({
          who = _player,
          num = _player.maxHp // 2,
          skillName = "qunxian3__hanniba_yanqing",
          recoverBy = player
        })
        room:changeMaxHp(_player, 3)
      elseif choice == "煎蛋" then
        if player:isFriend(_player) then
          local general = Fk:getGeneralsRandomly(1)
          local skill = table.random(general[1]:getSkillNameList())
          room:addTableMark(_player, "qunxian3__hanniba_yanqing-jiandan", skill)
          room:handleAddLoseSkills(_player, skill)
        elseif #_player:getSkillNameList() > 0 then
          room:setPlayerMark(_player, "qunxian3__hanniba_yanqing-jiandan_bad-turn", 1)
          local skillName = table.random(_player:getSkillNameList())
          room:handleAddLoseSkills(_player, "-" .. skillName)
        end
      else
        if not table.contains(_player:getTableMark("@[qunxian3__hanniba_yanqing]", choice)) then
          room:addTableMark(_player, "@[qunxian3__hanniba_yanqing]", choice)
        end
      end
    end
    room:setPlayerMark(player, "@[qunxian3__hanniba_pengren]", 0)
  end
})

local function getSuitString(suit)
  local ret = "unknown"
  if suit == Card.Spade then
    ret = "spade"
  elseif suit == Card.Heart then
    ret = "heart"
  elseif suit == Card.Club then
    ret = "club"
  elseif suit == Card.Diamond then
    ret = "diamond"
  elseif suit == Card.NoSuit then
    ret = "nosuit"
  end
  return ret
end

qunxian3__hanniba_yanqing:addEffect(fk.CardUsing, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill("qunxian3__hanniba_yanqing") and
        (table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "炸翅膀") or
          table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "豆腐花"))
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "豆腐花") then
      if player:isFriend(target) then
        local suits = {}
        for i = 1, 4 do
          table.insertIfNeed(suits, getSuitString(i))
        end
        table.insertIfNeed(suits, "Cancel")
        local choices = {}
        for index, cid in ipairs(target:getCardIds("h")) do
          table.insertIfNeed(choices, Fk:getCardById(cid):getSuitString())
        end
        table.insertIfNeed(choices, "Cancel")
        local discard = room:askToChoice(target, {
          choices = choices,
          skill_name = "qunxian3__hanniba_yanqing",
          cancelable = true,
          all_choices = suits,
          prompt = "#qunxian3__hanniba_yanqing-friend_doufuhua"
        })
        if discard ~= "Cancel" then
          local cards = table.filter(target:getCardIds("h"), function(cid, index, array)
            return Fk:getCardById(cid):getSuitString() == discard
          end)
          room:throwCard(cards, "qunxian3__hanniba_yanqing", target, target)

          local allCardIds, from = room.draw_pile, target
          local allCardMapper = {} ---@type table<string, integer[]>
          local allCardNames = {}
          for _, id in ipairs(allCardIds) do
            local card = Fk:getCardById(id)
            if card.type == Card.TypeEquip then
              if not allCardMapper[card.name] then
                allCardMapper[card.name] = {}
                table.insert(allCardNames, card.name)
              end
              if room:getCardOwner(id) ~= from then
                table.insert(allCardMapper[card.name], id)
              end
            end
          end

          if #allCardNames == 0 then
            return
          end

          local cardName = room:askToChoice(from, { choices = allCardNames, skill_name = "qunxian3__hanniba_yanqing" })
          local toGain
          if #allCardMapper[cardName] > 0 then
            toGain = table.random(allCardMapper[cardName])
          end
          room:obtainCard(target, toGain, true, fk.ReasonPrey, target, "qunxian3__hanniba_yanqing")
        end
      else
        room:loseHp(target, target.maxHp // 5 < 1 and 1 or target.maxHp // 5, "qunxian3__hanniba_yanqing")
      end
    elseif table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "炸翅膀") and player:isEnemy(target) and #target:getCardIds("he") > 0 then
      room:throwCard(table.random(target:getCardIds("he")), "qunxian3__hanniba_yanqing", target, player)
    end
  end
})

qunxian3__hanniba_yanqing:addEffect(fk.PreCardEffect, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target and player:hasSkill("qunxian3__hanniba_yanqing") and player:isEnemy(target) and
        table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "炸翅膀")
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(target, {
      min_num = 2,
      max_num = 2,
      cancelable = true,
      skill_name = "qunxian3__hanniba_yanqing",
      prompt = "#qunxian3__hanniba_yanqing-enemy_zhachibang",
      include_equip = true
    })
    if #cards ~= 2 then
      data.nullified = true
    else
      room:throwCard(cards, "qunxian3__hanniba_yanqing", target, target)
    end
  end
})

qunxian3__hanniba_yanqing:addEffect(fk.EventPhaseEnd, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "炸翅膀") and not target:isNude() and
        player:hasSkill("qunxian3__hanniba_yanqing") and player:isFriend(target) and target.phase == Player.Draw
        and table.find(player:getHandlyIds(),function(cid) 
          return Fk:getCardById(cid).type==Card.TypeEquip
        end)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToChooseCards(target, {
      min = 1,
      max = 4,
      pattern = ".|.|.|.|.|equip",
      cancelable = true,
      flag = "h",
      target = target,
      skill_name = "qunxian3__hanniba_yanqing",
      prompt = "#qunxian3__hanniba_yanqing-zhachibang_good"
    })
    if #cards > 0 then
      room:throwCard(cards, "qunxian3__hanniba_yanqing", target, target)
      for _, id in ipairs(cards) do
        local card = Fk:getCardById(id)
        if card.equip_skill and not target:hasSkill(card.equip_skill.name) then
          room:addTableMark(target, "qunxian3__hanniba_yanqing-zhachibang_good", card.id)
          room:addTableMark(target, "@[:]qunxian3__hanniba_yanqing-zhachibang_good", card.name)
          room:handleAddLoseSkills(target, card.equip_skill.name, nil, false, true)
        end
      end
    end
    room:removeTableMark(target, "@[qunxian3__hanniba_yanqing]", "炸翅膀")
  end
})

qunxian3__hanniba_yanqing:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:getTableMark("@[:]qunxian3__hanniba_yanqing-zhachibang_good") ~= {} or
        player:getTableMark("qunxian3__hanniba_yanqing-jiandan") ~= {}
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:getTableMark("@[:]qunxian3__hanniba_yanqing-zhachibang_good") ~= {} then
      for _, cid in ipairs(player:getTableMark("qunxian3__hanniba_yanqing-zhachibang_good")) do
        local card = Fk:getCardById(cid)
        if card.equip_skill and player:hasSkill(card.equip_skill.name, true) then
          room:handleAddLoseSkills(player, "-" .. card.equip_skill.name, nil, false, true)
        end
      end
      room:setPlayerMark(player, "@[:]qunxian3__hanniba_yanqing-zhachibang_good", 0)
      room:setPlayerMark(player, "qunxian3__hanniba_yanqing-zhachibang_good", 0)
    end
    if player:getTableMark("qunxian3__hanniba_yanqing-jiandan") ~= {} then
      local skillNames = player:getTableMark("qunxian3__hanniba_yanqing-jiandan")
      for _, skillName in ipairs(skillNames) do
        if player:hasSkill(skillName, true) then
          room:handleAddLoseSkills(player, "-" .. skillName)
        end
      end
      room:setPlayerMark(player, "qunxian3__hanniba_yanqing-jiandan", 0)
    end
  end
})

qunxian3__hanniba_yanqing:addEffect(fk.TurnStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return table.contains(target:getTableMark("@[qunxian3__hanniba_yanqing]"), "牛排") and
        player:hasSkill("qunxian3__hanniba_yanqing") and #target:getCardIds("h") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(target, {
      max_num = 1,
      min_num = 1,
      skill_name = "qunxian3__hanniba_yanqing",
      prompt = "#qunxian3__hanniba_yanqing-niupai_bad",
      include_equip = true,
      cancelable = false
    })
    event:setCostData(self, { cards = cards })
    return #cards > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local prohibit_card = Fk:getCardById(cards[1]).type
    local discards = table.filter(target:getCardIds("h"), function(cid, index, array)
      return Fk:getCardById(cid).type == prohibit_card
    end)
    room:throwCard(discards, "qunxian3__hanniba_yanqing", target, player)
    room:setPlayerMark(target, "qunxian3__hanniba_yanqing-niupai_prohibit-turn", prohibit_card)
  end
})

qunxian3__hanniba_yanqing:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    return from:distanceTo(to) > 1 and from:getMark("qunxian3__hanniba_yanqing-jiandan_bad-turn") > 0
  end,
  prohibit_use = function(self, player, card)
    return player:getMark("qunxian3__hanniba_yanqing-niupai_prohibit-turn") ~= 0 and card
        and card.type == player:getMark("qunxian3__hanniba_yanqing-niupai_prohibit-turn")
  end
})

qunxian3__hanniba_yanqing:addEffect(fk.EventPhaseStart, {
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill("qunxian3__hanniba_yanqing") and player.phase == Player.Play
        and data.reason == "game_rule"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for index, p in ipairs(room.alive_players) do
      if p:getMark("@[qunxian3__hanniba_yanqing]") ~= 0 then
        room:setPlayerMark(p, "@[qunxian3__hanniba_yanqing]", 0)
      end
    end
  end
})

Fk:addQmlMark {
  name = "qunxian3__hanniba_yanqing",
  how_to_show = function(name, value)
    if type(value) == "table" then
      return tostring(#value)
    end
    return " "
  end,
  qml_path = "packages/th_jie/qml/ZhidiBox"
}


return qunxian3__hanniba_yanqing
